package com.test.controller;


import com.test.bean.Result;
import com.test.bean.Stu_Record;
import com.test.bean.Student;
import com.test.service.UserService;
import com.test.utils.ImportExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("record")
public class RecordController {

    @Autowired
    private UserService userService;

//    档案信息单条录入
    @ResponseBody
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result AddRecord(Stu_Record record, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        try {
            userService.addRecord(record);
            result.setMessage("录入成功");
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

    //    档案信息批量录入
    @ResponseBody
    @RequestMapping(value = "adds",method = RequestMethod.POST)
    public class LeadingExcelController {

        /**
         * 描述：通过传统方式form表单提交方式导入excel文件
         *
         * @param request
         * @throws Exception
         */
        @RequestMapping("/form")
        public String form(HttpServletRequest request) throws Exception {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            InputStream in = null;
            List<List<Object>> listob = null;
            MultipartFile file = multipartRequest.getFile("upfile");

            if (file.isEmpty()) {
                throw new Exception("文件不存在！");
            }
            in = file.getInputStream();
            listob = new ImportExcelUtil().getBankListByExcel(in, file.getOriginalFilename());
            in.close();

            //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
            for (int i = 0; i < listob.size(); i++) {
                List<Object> lo = listob.get(i);
                Stu_Record record = new Stu_Record();
                record.setId(Integer.parseInt(String.valueOf(lo.get(0))));
                record.setName(String.valueOf(lo.get(1)));
                record.setRecord_place(String.valueOf(lo.get(2)));
                record.setRecord_status(String.valueOf(lo.get(3)));
                record.setRecord_time(String.valueOf(lo.get(4)));
                record.setRecord_unit(String.valueOf(lo.get(5)));
                record.setHeir_name(String.valueOf(lo.get(6)));
                record.setHeir_tel(String.valueOf(lo.get(7)));

            }

            return null;
        }

        /**
         * 描述：通过 jquery.form.js 插件提供的ajax方式上传文件
         * @param request
         * @param response
         * @throws Exception
         */
        @ResponseBody
        @RequestMapping(value="ajaxUpload.do",method={RequestMethod.GET,RequestMethod.POST})
        public  Result  ajaxUploadExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            Result result = new Result();
    //            System.out.println("通过 jquery.form.js 提供的ajax方式上传文件！");

            InputStream in =null;
            List<List<Object>> listob = null;
            MultipartFile file = multipartRequest.getFile("upfile");
            if(file.isEmpty()){
                result.setCode(400);
                result.setMessage("excel解析录入失败");
            }

            in = file.getInputStream();
            listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());

            //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
            for (int i = 0; i < listob.size(); i++) {
                List<Object> lo = listob.get(i);
                Stu_Record record = new Stu_Record();
                record.setId(Integer.parseInt(String.valueOf(lo.get(0))));
                record.setName(String.valueOf(lo.get(1)));
                record.setRecord_place(String.valueOf(lo.get(2)));
                record.setRecord_status(String.valueOf(lo.get(3)));
                record.setRecord_time(String.valueOf(lo.get(4)));
                record.setRecord_unit(String.valueOf(lo.get(5)));
                record.setHeir_name(String.valueOf(lo.get(6)));
                record.setHeir_tel(String.valueOf(lo.get(7)));

    //                System.out.println("打印信息-->机构:"+vo.getCode()+"  名称："+vo.getName()+"   时间："+vo.getDate()+"   资产："+vo.getMoney());
            }

            result.setCode(200);
            result.setMessage("excel解析录入成功");
            PrintWriter out = null;
            response.setCharacterEncoding("utf-8");  //防止ajax接受到的中文信息乱码
            out = response.getWriter();
    //            out.print("文件导入成功！");
            out.flush();
            out.close();
            return result;
        }
    }

//    档案信息查询
    @ResponseBody
    @RequestMapping(value = "query",method = RequestMethod.GET)
    public List<Stu_Record> QueryRecord(int id, HttpSession session, HttpServletResponse response)
    {
        Result result = new Result();
        List<Stu_Record> list = new ArrayList<Stu_Record>();
        try {
            list = userService.queryRecord(id);
            result.setMessage("删除成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return list;

    }
}
